﻿using AppService.IService.Basic_information;
using Domain.Entity.Basic_information;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WebAPI.Controllers.Basic_information
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class VehiclesController : ControllerBase
    {

        public IVehiclesService vehiclesService { get; set; }

        /// <summary>
        /// 分页查询车辆信息
        /// </summary>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">页数</param>
        /// <param name="date1">起始时间</param>
        /// <param name="date2">终止时间</param>
        /// <param name="value2">车主姓名</param>
        /// <returns></returns>
        [HttpGet("{currentPage},{pageSize}")]
        public List<Vehicles_ment> GetVehicles(int currentPage, int pageSize, string? date1 = null, string? date2 = null, string? value2 = null)
        {
            return vehiclesService.GetVehicles(currentPage, pageSize, date1, date2, value2);
        }

        /// <summary>
        /// 获取车辆数量
        /// </summary>
        /// <param name="date1">起始时间</param>
        /// <param name="date2">终止时间</param>
        /// <param name="value2">车主姓名</param>
        /// <returns></returns>
        [HttpGet]
        public int GetCount(string? date1, string? date2, string? value2)
        {
            return vehiclesService.GetCount(date1, date2, value2);
        }

        /// <summary>
        /// 删除车辆信息
        /// </summary>
        /// <param name="id">车辆id</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public bool DelVehicles(int id)
        {
            return vehiclesService.DelVehicles(id);
        }

        /// <summary>
        /// 添加车辆信息
        /// </summary>
        /// <param name="vehicles">车辆类</param>
        /// <returns></returns>
        [HttpPost("register")]
        public bool AddVehicles([FromBody] Vehicles_ment vehicles)
        {
            return vehiclesService.AddVehicles(vehicles);
        }

        /// <summary>
        /// 根据编号查询车辆
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public IActionResult GetById(int id)
        {
            if (id <= 0)
            {
                return BadRequest(new { message= "无效的车辆ID", data= -1 });
            }

            Vehicles_ment vehicle = vehiclesService.GetVehicles(id);

            if (vehicle == null)
            {
                return NotFound(new { message= $"未找到 ID 为 {id} 的车辆", data= -1 });
            }

            return Ok(vehicle);
        }

        /// <summary>
        /// 编辑部门
        /// </summary>
        /// <param name="vehicles"></param>
        /// <returns></returns>
        [HttpPut("register")]
        public IActionResult UpdVehicles([FromBody] Vehicles_ment vehicles)
        {
            bool flag = vehiclesService.UpdVehicles(vehicles);
            return Ok(flag);
        }

    }
}
